#!/usr/bin/env bash
##############################
configure_tmoe_container_main() {
    case $1 in
    dns) tmoe_container_dns ;;
    -menu | *) configure_tmoe_container ;;
    esac
}
#############
configure_tmoe_container() {
    RETURN_TO_WHERE='configure_tmoe_container'
    RETURN_TO_MENU='configure_tmoe_container'
    TMOE_OPTION=$(whiptail --title "CONF AND MANUAL" --menu "You can view the manual or modify the container configuration here." 0 50 0 \
        "1" "DNS 域名解析系统" \
        "2" "Timezone 时区" \
        "3" "hitokoto 一言" \
        "4" "man mount" \
        "5" "man umount" \
        "6" "man x11vnc" \
        "0" "🌚 Back 返回" \
        3>&1 1>&2 2>&3)
    ##############################
    case "${TMOE_OPTION}" in
    0 | "") proot_container_menu ;;
    1) tmoe_container_dns ;;
    2) tmoe_container_timezone ;;
    3) do_you_want_to_enable_hitokoto ;;
    4) man_mount ;;
    5) man_umount ;;
    6) man_x11vnc ;;
    esac
    ####################
    press_enter_to_return
    configure_tmoe_container
}
##########
check_man_db() {
    [[ $(command -v man) ]] || ${TMOE_INSTALLATION_COMMAND} man
    [[ $(command -v man) ]] || ${TMOE_INSTALLATION_COMMAND} man-db
    [[ ! $(command -v man) ]] || check_x11vnc_git_manual
}
#########
check_x11vnc_git_manual() {
    if [[ ! -e ${TMOE_LINUX_DIR}/docs/.git ]]; then
        mkdir -pv ${TMOE_LINUX_DIR}
        git clone --depth=1 -b main https://gitee.com/ak2/manual ${TMOE_LINUX_DIR}/docs
    fi
}
#######
man_mount() {
    check_man_db
    man ${TMOE_LINUX_DIR}/docs/mount.8
}
man_umount() {
    check_man_db
    man ${TMOE_LINUX_DIR}/docs/umount.8
}
man_x11vnc() {
    check_man_db
    man ${TMOE_LINUX_DIR}/docs/x11vnc.1
}
how_to_customize_dns() {
    printf "%s\n %s\n" "When you enter the container, you can type ${GREEN}editor /etc/resolv.conf${RESET}" "or ${GREEN}nano /etc/resolv.conf${RESET} to modify the DNS configuration file."
    printf "%s\n" "本功能仅适用于容器初始化配置，如需手动修改，则请在容器内部输入${GREEN}nano /etc/resolv.conf${RESET}"
}
#########
tmoe_container_dns() {
    RETURN_TO_WHERE='tmoe_container_dns'
    DNS_NAME=''
    TMOE_OPTION=$(whiptail --title "Please choose DNS" --menu "默认为第一项\nYou can choose cloudflare dns." 0 50 0 \
        "1" "[240c::6666](CFIEC)" \
        "2" "[2606:4700:4700::1111](CloudFlare)" \
        "3" "[2001:4860:4860::8888](Google)" \
        "4" "[2400:3200::1](Ali)" \
        "5" "[2400:da00::6666](Baidu)" \
        "6" "[2001:da8::666](Tsinghua-Tuna)" \
        "7" "How to customize dns?" \
        "0" "🌚 Back 返回" \
        3>&1 1>&2 2>&3)
    ##############################
    case "${TMOE_OPTION}" in
    0 | "")
        case ${RETURN_TO_MENU} in
        "") configure_tmoe_container ;;
        *) ${RETURN_TO_MENU} ;;
        esac
        ;;
    1) DNS_NAME='cfiec' ;;
    2) DNS_NAME='cloudflare' ;;
    3) DNS_NAME='google' ;;
    4) DNS_NAME='ali' ;;
    5) DNS_NAME='baidu' ;;
    6) DNS_NAME='tuna' ;;
    7) how_to_customize_dns ;;
    esac
    [[ -z ${DNS_NAME} ]] || cp -vf ${TMOE_SHARE_DIR}/configuration/dns/${DNS_NAME} ${CONFIG_FOLDER}/default-dns.conf
    ####################
    press_enter_to_return
    case ${RETURN_TO_MENU} in
    "") tmoe_container_dns ;;
    *) ${RETURN_TO_MENU} ;;
    esac
}
##############
is_your_timezone_gmt8() {
    if (whiptail --title "Is your timezone GMT+8" --yes-button "YES" --no-button "NO" --yesno "當前時區是否爲東八區(GMT +8)?" 8 50); then
        TIMEZONE="GMT-8" #此處爲-8而非+8
    else
        tmoe_container_timezone
    fi
}
is_your_timezone_gmt9() {
    if (whiptail --title "Is your timezone GMT+9" --yes-button "YES" --no-button "NO" --yesno "GMT+9 = 東9區\nあなたのタイムゾーンは GMT +9 ですか" 9 50); then
        TIMEZONE="GMT-9"
    else
        tmoe_container_timezone
    fi
}
is_your_timezone_utc() {
    if (whiptail --title "TIMEZONE" --yes-button "YES" --no-button "NO" --yesno "Is your timezone UTC?" 8 50); then
        TIMEZONE="UTC"
    else
        tmoe_container_timezone
    fi
}
what_is_your_timezone() {
    case ${TMOE_MENU_LANG} in
    zh_*UTF-8) is_your_timezone_gmt8 ;;
    ja_JP.UTF-8) is_your_timezone_gmt9 ;;
    *) is_your_timezone_utc ;;
    esac
    mkdir -pv ${CONFIG_FOLDER}/gitstatus
    [[ -z ${TIMEZONE} ]] || printf "%s\n" "Etc/${TIMEZONE}" >${CONFIG_FOLDER}/gitstatus/timezone.txt
}
tmoe_container_timezone() {
    RETURN_TO_WHERE='tmoe_container_timezone'
    unset TIMEZONE IMPORTANT_TIPS_03
    TIMEZONE=$(cat ${CONFIG_FOLDER}/gitstatus/timezone.txt | tr '[+\-]' '[\-+]') #管道
    [[ -z ${TIMEZONE} ]] || IMPORTANT_TIPS_03="Current timzone of container is ${TIMEZONE}"
    TMOE_OPTION=$(whiptail --title "Please choose TIMEZONE" --menu "GMT is Greenwich Mean Time.\nGMT+8 = 東8區\n${IMPORTANT_TIPS_03}" 0 50 0 \
        "0" "🌚 Back" \
        "1" "GMT+1" \
        "2" "GMT+2" \
        "3" "GMT+3" \
        "4" "GMT+4" \
        "5" "GMT+5" \
        "6" "GMT+6" \
        "7" "GMT+7" \
        "8" "GMT+8" \
        "9" "GMT+9" \
        "10" "GMT+10" \
        "11" "GMT+11" \
        "12" "GMT+12" \
        "13" "GMT+13" \
        "14" "GMT+14" \
        "15" "UTC" \
        "16" "GMT-1" \
        "17" "GMT-2" \
        "18" "GMT-3" \
        "19" "GMT-4" \
        "20" "GMT-5" \
        "21" "GMT-6" \
        "22" "GMT-7" \
        "23" "GMT-8" \
        "24" "GMT-9" \
        "25" "GMT-10" \
        "26" "GMT-11" \
        "27" "GMT-12" \
        3>&1 1>&2 2>&3)
    #LINUX Etc/GMT-8为GMT--8,即为GMT+8
    ##############################
    case "${TMOE_OPTION}" in
    0 | "")
        case ${RETURN_TO_MENU} in
        "") configure_tmoe_container ;;
        *) ${RETURN_TO_MENU} ;;
        esac
        ;;
    1) TIMEZONE='GMT-1' ;;
    2) TIMEZONE='GMT-2' ;;
    3) TIMEZONE='GMT-3' ;;
    4) TIMEZONE='GMT-4' ;;
    5) TIMEZONE='GMT-5' ;;
    6) TIMEZONE='GMT-6' ;;
    7) TIMEZONE='GMT-7' ;;
    8) TIMEZONE='GMT-8' ;;
    9) TIMEZONE='GMT-9' ;;
    10) TIMEZONE='GMT-10' ;;
    11) TIMEZONE='GMT-11' ;;
    12) TIMEZONE='GMT-12' ;;
    13) TIMEZONE='GMT-13' ;;
    14) TIMEZONE='GMT-14' ;;
    15) TIMEZONE='UTC' ;;
    16) TIMEZONE='GMT+1' ;;
    17) TIMEZONE='GMT+2' ;;
    18) TIMEZONE='GMT+3' ;;
    19) TIMEZONE='GMT+4' ;;
    20) TIMEZONE='GMT+5' ;;
    21) TIMEZONE='GMT+6' ;;
    22) TIMEZONE='GMT+7' ;;
    23) TIMEZONE='GMT+8' ;;
    24) TIMEZONE='GMT+9' ;;
    25) TIMEZONE='GMT+10' ;;
    26) TIMEZONE='GMT+11' ;;
    27) TIMEZONE='GMT+12' ;;
    esac
    mkdir -pv ${CONFIG_FOLDER}/gitstatus
    [[ -z ${TIMEZONE} ]] || printf "%s\n" "Etc/${TIMEZONE}" >${CONFIG_FOLDER}/gitstatus/timezone.txt
    sed -n p ${CONFIG_FOLDER}/gitstatus/timezone.txt | tr '[+\-]' '[\-+]'
    ####################
    press_enter_to_return
    case ${RETURN_TO_MENU} in
    "") tmoe_container_timezone ;;
    *) ${RETURN_TO_MENU} ;;
    esac
}
install_fortunes() {
    if [[ ! $(command -v fortune) ]] && [ ! -e /usr/games/fortune ]; then
        printf "${GREEN}%s ${PURPLE}%s${RESET}\n" "${TMOE_INSTALLATION_COMMAND}" "fortune"
        ${TMOE_INSTALLATION_COMMAND} fortunes 2>/dev/null
        ${TMOE_INSTALLATION_COMMAND} fortune-mod 2>/dev/null
        ${TMOE_INSTALLATION_COMMAND} fortune 2>/dev/null
        case ${TMOE_LANG} in
        zh_*UTF-8)
            ${TMOE_INSTALLATION_COMMAND} fortunes-zh 2>/dev/null
            install_fortunes_cn
            ;;
        esac
    fi
    printf "${YELLOW}%s${RESET}\n" "Enable completed."
}
install_fortunes_cn() {
    case ${LINUX_DISTRO} in
    Android)
        THE_LATEST_DEB_REPO='https://mirrors.bfsu.edu.cn/debian/pool/main/f/fortune-zh/'
        THE_LATEST_DEB_VERSION="$(curl -L ${THE_LATEST_DEB_REPO} | egrep -v '\.dsc|\.tar\.xz' | grep 'deb' | tail -n 1 | awk -F '=' '{print $3}' | cut -d '"' -f 2)"
        THE_LATEST_ISO_LINK="${THE_LATEST_DEB_REPO}${THE_LATEST_DEB_VERSION}"
        TEMP_FOLDER=${TMPDIR}/.FORTUNES_TMP
        mkdir -pv ${TEMP_FOLDER}
        cd ${TEMP_FOLDER}
        aria2c --console-log-level=warn --no-conf --allow-overwrite=true -o ${THE_LATEST_DEB_VERSION} "${THE_LATEST_ISO_LINK}"
        dpkg-deb -X ${THE_LATEST_DEB_VERSION} ./
        cd usr/share/games/fortunes
        for i in $(readlink ./*); do
            cp -vf ${i} ${PREFIX}/share/games/fortunes
        done
        cd ${CURRENT_DIR} || cd ${HOME}
        rm -rv ${TEMP_FOLDER}
        ;;
    esac
}
##############
do_you_want_to_enable_hitokoto() {
    if (whiptail --title "HITOKOTO AND FORTUNE" --yes-button "YES" --no-button "NO" --yesno "DO YOU WANT TO ENABLE HITOKOTO?\n是否需要启用一言？♪(^∇^*) " 9 50); then
        cat >${CONFIG_FOLDER}/hitokoto.conf <<-EOF
			TMOE_CONTAINER_FORTUNE=true
			TMOE_CONTAINER_HITOKOTO=true
		EOF
        install_fortunes
    else
        cat >${CONFIG_FOLDER}/hitokoto.conf <<-EOF
			TMOE_CONTAINER_FORTUNE=false
			TMOE_CONTAINER_HITOKOTO=false
		EOF
        if [[ $(command -v fortune) ]] || [ -e /usr/games/fortune ]; then
            ${TMOE_REMOVAL_COMMAND} fortune
            ${TMOE_REMOVAL_COMMAND} fortunes
            ${TMOE_REMOVAL_COMMAND} fortunes-zh
        fi
    fi
}
###########
#不包含$@
